package com.xinghou.mq.consumer;

import com.aliyun.openservices.ons.api.Message;
import com.xinghou.mq.consumer.util.LoggerFactory;
import com.xinghou.platform.ienum.ConsumerEnum;
import com.xinghou.platform.server.mq.MqConstant;
import com.xinghou.platform.util.StringUtils;
import org.slf4j.Logger;

import javax.annotation.Resource;

/**
 * Created by qingshengzheng on 16/4/28.
 */
public class ConsumerImpl implements IConsumer {

    private Logger logger = LoggerFactory.getServerInfoLogger(ConsumerImpl.class);
    private Logger loggerError = LoggerFactory.getServerErrorLogger(ConsumerImpl.class);
    private CardConsumer cardConsumer;
    private ExposureConsumer exposureConsumer;
    private StoreConsumer storeConsumer;
    private InterestConsumer interestConsumer;
    private UserConsumer userConsumer;

    @Override
    public void execute(Message message) {
        if (cardConsumer == null) initConsumer();
        try {
            if (StringUtils.isEmpty(message.getKey())) return;
            int bizcode = Integer.parseInt(message.getKey());
            if (MqConstant.MQ_BIZ_CARD_UPDATE == bizcode) {
                cardConsumer.doWork(new String(message.getBody()));
            } else if (MqConstant.MQ_BIZ_EXPOSURE_UPDATE == bizcode) {
                exposureConsumer.doWork(new String(message.getBody()));
            } else if (MqConstant.MQ_BIZ_USER_UPDATE == bizcode) {
                userConsumer.doWork(new String(message.getBody()));
            } else if (MqConstant.MQ_BIZ_INTEREST_UPDATE == bizcode) {
                interestConsumer.doWork(new String(message.getBody()));
            } else if (MqConstant.MQ_BIZ_STORE_UPDATE == bizcode) {
                storeConsumer.doWork(new String(message.getBody()));
            } else if (MqConstant.MQ_BIZ_HXREGISTER == bizcode) {
                userConsumer.doHuanxinRegister(new String(message.getBody()));
            } else if (MqConstant.MQ_BIZ_HISTORY_CREATE == bizcode) {
                userConsumer.addUserHistorey(new String(message.getBody()));
            } else if (MqConstant.MQ_BIZ_UPDATE_GPS == bizcode) {
                userConsumer.updateUserGPS(new String(message.getBody()));
            } else {
                logger.error("unknow bizcode[{}],body[{}]!", message.getKey(), message.getBody());
            }
        } catch (Exception e) {
            e.printStackTrace();
            loggerError.error("message exception", e);
            throw  e;

        }
    }

    public void initConsumer() {
        cardConsumer = new CardConsumer();
        exposureConsumer = new ExposureConsumer();
        storeConsumer = new StoreConsumer();
        interestConsumer = new InterestConsumer();
        userConsumer = new UserConsumer();
    }
}
